package com.tianya.common.taglib;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.*;
import com.tianya.common.util.Utility;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
public class WriteDateLabelTag extends javax.servlet.jsp.tagext.SimpleTagSupport {

	private String inputFormat;
	private String outputFormat;
	@SuppressWarnings("unused")
	private boolean isSys = false;
	@SuppressWarnings("unused")
	private String style;
	private String dateValue;
	private String fullDate = "false";
	
	public void setDateValue(String dateValue) {
		this.dateValue = dateValue;
	}

	public void setInputFormat(String inputFormat) {
		this.inputFormat = inputFormat;
	}

	public void setOutputFormat(String outputFormat) {
		this.outputFormat = outputFormat;
	}

	public void doTag() throws JspException, IOException {
		if (StringUtils.isEmpty(inputFormat)) {
			//默认的输入值，从数据串过来的，所以是没有格式的。
			//Utility.getInputDateFormat是指画面输入时候的格式，
			inputFormat = fullDate.equalsIgnoreCase("true") ? "yyyyMMddHHmmss":(fullDate.equalsIgnoreCase("short")?"MMdd":"yyyyMMdd");
		}
//		//获取输出格式
//		if(StringUtils.isEmpty(outputFormat)){
//			outputFormat =  fullDate.equalsIgnoreCase("true") ? Utility.getFullDateFormat(isSys):
//				(fullDate.equalsIgnoreCase("short")?Utility.getMdDateFormat(isSys):Utility.getDateFormat(isSys));
//		}
		//日历框的特殊处理
//		if(!StringUtils.isEmpty(style))
//		{
//			//日历框
//			if(style.equalsIgnoreCase("calendar") || style.equalsIgnoreCase("c")){
//				StringBuffer sb = new StringBuffer();
//				HttpServletRequest  request  = (HttpServletRequest )((PageContext) getJspContext()).getRequest(); 
//				String strContext ="";
//				if(request != null){
//					
//					strContext = request.getContextPath();
//				}
//				sb.append("<script type=\"text/javascript\" src=\"").append(strContext).append("/js/calendar.js\"></script>");
//				sb.append("<script>WebCalendar.format='").append(Utility.getDateInputFormat(false)).append("';</script>");
//				this.getJspContext().getOut().write(sb.toString());
//				return;
//			}
//			if(style.equalsIgnoreCase("i")||style.equalsIgnoreCase("input")){
//				outputFormat =  fullDate.equalsIgnoreCase("true") ? Utility.getFullDateInputFormat(isSys):
//					(fullDate.equalsIgnoreCase("short")?Utility.getMdDateInputFormat(isSys):Utility.getDateInputFormat(isSys));
//			}
//		}
		
		
		try {
			Date date = null;
			if("date".equalsIgnoreCase(inputFormat)){
				if(!Utility.isEmpty(dateValue)){
					date = Utility.parseStringToDate(dateValue);
				}
			}
			else{
				if(!Utility.isEmpty(dateValue)){
					date = DateUtils.parseDate(dateValue, new String[] { inputFormat });
				}
			}
			SimpleDateFormat formater = new SimpleDateFormat(outputFormat);   
			if(date!=null){
				this.getJspContext().getOut().write(formater.format(date));
			}else{
				this.getJspContext().getOut().write("");
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public void setSys(boolean isSys) {
		this.isSys = isSys;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String setFullDate() {
		return fullDate;
	}

	public void setFullDate(String fullDate) {
		this.fullDate = fullDate;
	}

}